package com.cll.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.cll.mall.product.entity.AttrAttrgroupRelationEntity;
import com.cll.mall.product.entity.AttrEntity;
import com.cll.mall.product.service.AttrAttrgroupRelationService;
import com.cll.mall.product.service.AttrService;
import com.cll.mall.product.service.CategoryService;
import com.cll.mall.product.vo.AttrGroupRelationVo;
import com.cll.mall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.cll.mall.product.entity.AttrGroupEntity;
import com.cll.mall.product.service.AttrGroupService;
import com.cll.common.utils.PageUtils;
import com.cll.common.utils.R;


/**
 * 属性分组
 *
 * @author cll
 * @email cll@gmail.com
 * @date 2025-06-08 17:52:24
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService catelogoryService;

    @Autowired
    private AttrService attrService;
    @Autowired
    AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 删除关联的分组
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos){
        attrService.deleteRelation(vos);

        return R.ok();
    }

    /**
     * 获取分类下所有分组&关联属性
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId) {

        // 查出当前分类下的所有属性分组
        List<AttrGroupWithAttrsVo> results = attrGroupService.getAttrGroupWithAttrs(catelogId);
        // 查出每个分组的所有属性

        return R.ok().put("data", results);
    }
    /**
     * 获取属性分组有关联的其他属性
     *
     * @param attrgroupId
     * @return
     */
    @GetMapping(value = "/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId) {

        List<AttrEntity> entities = attrService.getRelationAttr(attrgroupId);

        return R.ok().put("data", entities);
    }


    /**
     * 获取属性分组没有关联的其他属性
     */
    @GetMapping(value = "/{attrgroupId}/noattr/relation")
    public R attrNoattrRelation(@RequestParam Map<String, Object> params,
                                @PathVariable("attrgroupId") Long attrgroupId) {

        PageUtils page = attrService.getNoRelationAttr(params, attrgroupId);

        return R.ok().put("page", page);
    }
    /**
     * 批量保存
     */
    @RequestMapping("/attr/relation")
    public R save(@RequestBody List<AttrAttrgroupRelationEntity> attrAttrgroupRelation) {
        attrAttrgroupRelationService.saveBatch(attrAttrgroupRelation);

        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable(value = "catelogId") Long catelogId) {
        PageUtils page = attrGroupService.queryPage(params, catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        Long[] catelogPath = catelogoryService.getPath(catelogId);
        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
